package liuch.socketdemov1;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 * 功能描述: socket client端<br>
 * 〈功能详细描述〉client 控制台输入，服务端输出
 *
 * @data 2018/8/2/002
 * @author 17101651
 */
public class SocketClientDemo {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 8088);
        InetAddress inetAddress = socket.getLocalAddress();
        System.out.println(inetAddress.getHostAddress());

        try {
            // 通讯
            OutputStream outputStream = socket.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
            PrintWriter pw = new PrintWriter(outputStreamWriter, true);
            Scanner scanner = new Scanner(System.in);
            while (true){
                pw.println(scanner.nextLine());
            }

            // 接收
//            InputStream inputStream = socket.getInputStream();
//            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
//            BufferedReader bf = new BufferedReader(inputStreamReader);
//            System.out.println("服务端说：" + bf.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

